package io.renren.modules.pet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.ConvertUtils;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.pet.dao.PetTeamCollectDao;
import io.renren.modules.pet.dto.PetCollectDTO;
import io.renren.modules.pet.entity.PetCollectEntity;
import io.renren.modules.pet.vo.PetTeamCollectVO;
import io.renren.modules.pet.dto.PetTeamCollectDTO;
import io.renren.modules.pet.entity.PetTeamCollectEntity;
import io.renren.modules.pet.service.PetTeamCollectService;
import io.renren.modules.security.user.SecurityUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 宠物团队收藏表
 *
 * @author zwp zwp
 * @since 1.0.0 2025-03-24
 */
@Service
public class PetTeamCollectServiceImpl extends CrudServiceImpl<PetTeamCollectDao, PetTeamCollectEntity, PetTeamCollectVO> implements PetTeamCollectService {

    @Override
    public QueryWrapper<PetTeamCollectEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<PetTeamCollectEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Override
    public void save(PetTeamCollectDTO petTeamCollectDTO) {
        PetTeamCollectEntity petTeamCollectEntity = ConvertUtils.sourceToTarget(petTeamCollectDTO, PetTeamCollectEntity.class);
        this.insert(petTeamCollectEntity);
    }

    @Override
    public void update(PetTeamCollectDTO petTeamCollectDTO) {
        PetTeamCollectEntity petTeamCollectEntity = ConvertUtils.sourceToTarget(petTeamCollectDTO, PetTeamCollectEntity.class);
        this.updateById(petTeamCollectEntity);
    }

    @Override
    public List<PetTeamCollectVO> getByPetTeamId(Long petTeamId) {
        QueryWrapper<PetTeamCollectEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("pet_team_id", petTeamId);
        List<PetTeamCollectEntity> entityList = this.baseDao.selectList(wrapper);
        return ConvertUtils.sourceToTarget(entityList, PetTeamCollectVO.class);
    }

    @Override
    public void cancelCollectTeam(PetTeamCollectDTO dto) {
        Long petTeamId = dto.getPetTeamId();
        // 获取当前用户信息
        Long userId = SecurityUser.getUserId();
        QueryWrapper<PetTeamCollectEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("pet_team_id", petTeamId);
        wrapper.eq("creator", userId);
        this.baseDao.delete(wrapper);
    }

    @Override
    public List<PetTeamCollectVO> getByUserId(Long userId) {
        QueryWrapper<PetTeamCollectEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("creator", userId);
        List<PetTeamCollectEntity> entityList = this.baseDao.selectList(wrapper);
        return ConvertUtils.sourceToTarget(entityList, PetTeamCollectVO.class);
    }


}